<?php
namespace store\oss;

if (is_file(__DIR__ . '/../../vendor/autoload.php')) {
    require_once __DIR__ . '/../../vendor/autoload.php';
}
use store\FileBlockWriter;
use store\StorageType;
use utils\SysTool;

class OSSWriter extends FileBlockWriter
{
    function __construct()
    {
        $this->storage = StorageType::Minio;
    }

	//创建文件,f_create.php调用
	function make(/*FileInf*/$file)
	{
		$id = OSSTool::createMultipartUpload($file->ossKey());
		return $id;
	}
	
	/**
	 * 向文件中写入块数据
	 * @param long $blockOffset
	 * @param long $lenLoc
	 * @param string $pathSvr
	 * @param string $tmpFile
	 */
	function write(/*FileInf*/$file,$data)
	{   
	    $file->etag = OSSTool::uploadPart($file, $data);	    
	    //保存etag信息
	    $file->saveEtags();
	    return $file->etag;
	}
	
	/**
	 * 向文件中写入块数据
	 * @param long $blockOffset
	 * @param long $lenLoc
	 * @param string $pathSvr
	 * @param string $tmpFile
	 */
	function writeData($blockOffset,$lenLoc,$pathSvr,$data)
	{
	    //写入数据
	    $sys = new SysTool();
	    $pathSvr = $sys->to_gbk($pathSvr);
	    $hfile = fopen($pathSvr,"r+b");
	    //定位到续传位置
	    fseek($hfile,intval($blockOffset),SEEK_SET);
	    fwrite($hfile,$data);
	    fclose($hfile);
	}
	
	function writeLastPart(/*FileInf*/$file)
	{
	    OSSTool::completeMultipartUpload($file);
	}
}
?>